package com.fire.core.service.impl;
import com.fire.core.dao.TMitErrorCodeMapper;
import com.fire.core.mode.TMitErrorCode;
import com.fire.core.service.ITMitErrorCodeService;
import com.fire.core.service.RedisService;
import com.fire.core.util.AjaxResult;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author yangxiaotian
 * @date 2018年5月28日 下午4:17:12
 * @description: 错误码提示语表 服务实现类
 * @version 1.0
 *
 */

@Service
public class TMitErrorCodeServiceImpl extends ServiceImpl<TMitErrorCodeMapper, TMitErrorCode>
		implements ITMitErrorCodeService {
	private static final Logger LOGGER = LoggerFactory.getLogger(TMitErrorCodeServiceImpl.class);
	@Autowired
	private TMitErrorCodeMapper tMitErrorCodeMapper;
	@Autowired
	private RedisService redisService;

	/**
	 * @Description:获取错误码友情信息提示方法(先查询缓存，缓存没数据再向数据库去查)
	 * @param: code
	 * @return: tips
	 */
	@Override
	public String getTips(String code) {
		String tips = null;
		Map<String, String> errorCodeList = (Map<String, String>) redisService.get("errorCodeMap");
		LOGGER.info("获取到的缓存错误码map" + errorCodeList);
		if (errorCodeList != null) {
			if (StringUtils.isNotBlank(errorCodeList.get(code))) {
				LOGGER.info("传入到的code" + code);
				tips = errorCodeList.get(code);
				LOGGER.info("通过code在缓存里获取到的数据" + tips);
			} else {
				tips = getTipsBydb(code);
			}
		} else {
			tips = getTipsBydb(code);

		}

		return tips;
	}

	/**
	 * 通过数据库获取错误码
	 * 
	 * @param bkey
	 * @return String value
	 * @throws Exception
	 */
	public String getTipsBydb(String code) {
		String tips = null;
		TMitErrorCode tMitErrorCode = tMitErrorCodeMapper.selectById(code);
		if (tMitErrorCode != null) {
			if (StringUtils.isNotBlank(tMitErrorCode.getTips())) {
				tips = tMitErrorCode.getTips().trim();
			} else {
				LOGGER.info("该错错误码信息不完善");
				return null;
			}

		} else {
			LOGGER.info("查询暂无该错误码信息");
			return null;
		}
		return tips;
	}

	/**
	 * 友情提示信息替换 (先查询缓存，缓存没数据再向数据库去查)
	 * 
	 * @param ajaxResult
	 * @return ajaxResult
	 */
	@Override
	public AjaxResult replaceMess(AjaxResult ajaxResult) {
		Map<String, String> errorCodeMap = (Map<String, String>) redisService.get("errorCodeMap");
		LOGGER.info("获取到的缓存错误码map" + errorCodeMap);
		if (ajaxResult == null) {
			LOGGER.info("返回实体为空");
			return null;
		} else if (errorCodeMap != null) {
			if (StringUtils.isNotBlank(errorCodeMap.get(ajaxResult.getStatus()))) {
				LOGGER.info("错误码友情提示信息已经替换");
				ajaxResult.setTips("[" + ajaxResult.getStatus() + "]" + errorCodeMap.get(ajaxResult.getStatus()));
			} else {
				TMitErrorCode tMitErrorCode = tMitErrorCodeMapper.selectById(ajaxResult.getStatus());
				if (tMitErrorCode == null) {
					LOGGER.info("查询暂无该错误码配置信息");
					ajaxResult.setTips("[" + ajaxResult.getStatus() + "]" + ajaxResult.getMessage());
				} else {
					if (StringUtils.isNotBlank(tMitErrorCode.getTips())) {
						LOGGER.info("错误码友情提示信息已经替换");
						ajaxResult.setTips("[" + ajaxResult.getStatus() + "]" + tMitErrorCode.getTips());
					} else {
						LOGGER.info("该错误码友情提示信息不完善");
						ajaxResult.setTips("[" + ajaxResult.getStatus() + "]" + ajaxResult.getMessage());
					}
				}
			}
		}
		return ajaxResult;
	}
}
